home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / PRINTF3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  1.6 KB  |  41 lines

  1. /*  printf3.c, from p. 451 of Turbo C Bible  */
  2. #include <stdio.h>
  3. char far  *strf = "Far string...";
  4. char near *strn = "Near string...";
  5. char *var_name[] = {"long_name_variable", "shorter_var", "short"};
  6. double values[] = {1.23, 3.4567, 9.87654321};
  7. unsigned int num_vars = sizeof(values)/sizeof(double);
  8. main()
  9. {
  10.     int i, j, numprint, chcount, width, precision = 0;
  11.     numprint = printf("Some special features of printf\n%n", &chcount);
  12.     printf("printf returned %d and character count in "
  13.        " variable is %d\n", numprint, chcount);
  14.                 /*  Use of addressing mode modifiers  */
  15.     printf("\nYou can print 'near' and 'far' data items properly:\n");
  16.     printf("Example: %Fs (far),\n%Ns (near) will print in any model\n",
  17.        strf, strn);
  18.                 /*  Printing addresses of variables  */
  19.     printf("\nYou can even print the addresses:\n");
  20.     printf("Item           Segment:Offset\n");
  21.     printf("'far'  string:    %Fp\n", (void far *)strf);
  22.     printf("'near' string:    %Fp\n", (void far *)strn);
  23.                 /*  Width and precision can be decided  */
  24.                 /*  at run-time                         */
  25.     printf("\nThe format can even be decided at run-time\n");
  26.     for (i = 0; i < num_vars; i++)
  27.     {
  28.                 /*  Find maximum length of variable names  */
  29.     if ((j = strlen(var_name[i])) > precision)
  30.         precision = j;
  31.     }
  32.                 /*  Make the width 4 characters longer  */
  33.                 /*  and print names left justified      */
  34.     width = precision + 4;
  35.     printf("--- Table of Variables ---\n");
  36.     for (i = 0; i < num_vars; i++)
  37.     {
  38.     printf("%-*.*s  %12.8f\n", width, precision, var_name[i],
  39.                                 values[i]);
  40.     }
  41. }